<template>
  <div class="page-container padding-20 query-list-container OrderDetails">
    <el-form
      class="el-form-modify"
      ref="form"
      label-width="80px"
      :model="formData"
      :rules="rules"
    >
      <el-form-item label="账号" prop="loginName">
        <el-input v-model="formData.loginName" placeholder="请输入" />
      </el-form-item>
      <el-form-item label="员工姓名" prop="name">
        <el-input placeholder="请输入" v-model="formData.name"/>
      </el-form-item>
      <el-form-item label="手机号" prop="phone">
        <el-input v-model="formData.phone" placeholder="请输入" />
      </el-form-item>
      <el-form-item label="电子邮箱" prop="email">
        <el-input v-model="formData.email" placeholder="请输入" />
      </el-form-item>
      <el-form-item label="角色" prop="roleIds" placeholder="请选择">
        <el-select v-model="formData.roleIds" :disabled="$route.query && $route.query.roleId" multiple style="width: 100%">
          <el-option v-for="role in roleList" :key="role.id" :value="role.id" :label="role.name"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="save" size="small" :loading="saving"
          >保存</el-button
        >
        <el-button @click="cancel" size="small">取消</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>

<script>
import { userAdd, userEdit, userDetail } from "@/api/user";
import {rules} from "@/utils/validate"

export default {
  name: "UserForm",
  components: {},
  props: ['roleList'],
  data() {
    let formData = {
      loginName: "",
      name: "",
      phone: '',
      email: '',
      roleIds: [],
    }

    return {
      formData,
      rules: {
        loginName: [rules.text2_18_char],
        name: [rules.required, rules.text2_18],
        phone: [rules.required, rules.phone],
        email: [rules.email],
        roleIds: [rules.select_required]
      },
      saving: false
    };
  },
  methods: {
    async requestDetail(id) {
      const {data} = await userDetail(id)
      this.formData = { ...data, accountId: id }
    },
    save() {
      this.$refs.form.validate(async valid => {
        if (!valid) {
          return;
        }
        try {
          this.saving = true;
          let postData = {...this.formData}
          if (!postData.loginName) {
            postData.loginName = postData.phone
          }
          console.log(this.formData)
          const method = postData.accountId ? userEdit : userAdd;
          await method(postData);
          this.$message.success('保存成功');
          this.saving = false;
          this.$emit("refresh");
          this.cancel();
        } catch (err) {
          this.saving = false;
        }
      });
    },
    cancel() {
      this.$router.go(-1)
    }
  },
  created() {
    if (this.$route.name == 'editUser') {
      let { id } = this.$route.params;
      if (id) {
        this.requestDetail(id);
      } else {
        this.cancel()
      }
    }
    if (this.$route.query && this.$route.query.roleId) {
      this.formData.roleIds = [this.$route.query.roleId]
    }
  }
};
</script>

<style lang="scss">
</style>
